//
//  SFStatusPictureView.swift
//  SFWeiboPro
//
//  Created by happy on 2017/9/22.
//  Copyright © 2017年 happy. All rights reserved.
//

import UIKit

class SFStatusPictureView: UIView {

    @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    
    var urls: [SFStatusPicture]? {
        
        didSet {
            
            for iv in subviews {
                iv.isHidden = true
            }
            
            var index = 0
            
            for statusPic in urls ?? [] {
                
                let iv = subviews[index] as! UIImageView
                
                if index == 1 && urls?.count == 4 {
                    index += 1
                }
                
                iv.sf_setImage(urlString: statusPic.thumbnail_pic, placeholderImage: nil)
                
                iv.isHidden = false
                
                index += 1
            }
            
        }
        
    }
    
    
    override func awakeFromNib() {
        
        setupUI()
        
    }
    
}

extension SFStatusPictureView {
    
    fileprivate func setupUI() {
        
        clipsToBounds = true
        
        backgroundColor = superview?.backgroundColor
        
        for i in 0..<9 {
            
            let iv = UIImageView()
            
            iv.contentMode = .scaleAspectFill
            iv.clipsToBounds = true
            
            let row = CGFloat(i/3)
            let line = CGFloat(i%3)
            
            let bothWidth = SFStatusPictureItemWidth + SFStatusPictureViewInnerMargin
            let ivY = SFStatusPictureViewOutterMargin + bothWidth * row
            
            iv.frame = CGRect(x: line * bothWidth, y: ivY, width: SFStatusPictureItemWidth, height: SFStatusPictureItemWidth)
            
            addSubview(iv)
        }
        
    }
    
}
